
package com.kugou.game.sdk.protocol;

import com.kugou.framework.base.AppException;
import com.kugou.framework.base.LogUtil;
import com.kugou.framework.http.KGHttpClient;
import com.kugou.framework.http.ResponsePackage;
import com.kugou.game.sdk.entity.GiftBag;
import com.kugou.game.sdk.entity.GiftbagListResponse;
import com.kugou.game.sdk.utils.ProtocolCmd;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 描述:获取礼包列表
 * 
 * @author liux
 * @since 2014-8-16 下午12:52:18
 */
public class RequestGiftbagTotalListTask {

    private ArrayList<String> mCmdList = new ArrayList<String>();

    public GiftbagListResponse request(int page, int pageSize, String username) {
        ArrayList<HashMap<String, Object>> requestParams = new ArrayList<HashMap<String, Object>>();
        // 填充参数，key-value。key是接口要求传的变量名称
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("cmd", ProtocolCmd.CMD_GIFT_LIST);
        params.put("page", page);
        params.put("pagesize", pageSize);
        params.put("username", username);
        HttpBaseRequestPackage request = new HttpBaseRequestPackage();
        request.setUserName(username);
        ResponsePackage responsePackage = new GiftbagListResponsePackage();
        GiftbagListResponse response = new GiftbagListResponse();
        requestParams.add(params);
        request.setRequestParams(requestParams);
        mCmdList.add(ProtocolCmd.CMD_GIFT_LIST);
        try {
            KGHttpClient.request(request, responsePackage);
            // 响应类赋值
            responsePackage.getResponseData(response);
            // 返回响应类
            return response;
        } catch (AppException ae) {
            ae.printStackTrace();
        }
        return null;
    }

    private class GiftbagListResponsePackage extends HttpBaseResponsePackage<GiftbagListResponse> {

        @Override
        protected void handleResponse(GiftbagListResponse response, String responseJsonStr) {
            try {
                LogUtil.d("response", "RequestGiftbagTotalListTask:" + responseJsonStr);
                JSONArray responseArray = new JSONArray(responseJsonStr);
                int size = responseArray.length();
                for (int i = 0; i < size; i++) {
                    if (i >= mCmdList.size()) {
                        break;
                    }
                    JSONObject jsonResp = responseArray.getJSONObject(i);
                    String cmd = jsonResp.optString("cmd");
                    String code = jsonResp.optString("code");
                    String message = jsonResp.optString("message");
                    String prompt = jsonResp.optString("prompt");
                    response.setCode(code);
                    response.setMessage(message);
                    response.setPrompt(prompt);
                    if (jsonResp.optInt("code") != 1) {
                        continue;
                    }
                    if (jsonResp.isNull("data")) {
                        continue;
                    }
                    if (ProtocolCmd.CMD_GIFT_LIST.equalsIgnoreCase(cmd)) {
                        JSONArray jsonResult = jsonResp.getJSONArray("data");
                        if (jsonResult != null) {
                            ArrayList<GiftBag> list = new ArrayList<GiftBag>();
                            for (int j = 0, length = jsonResult.length(); j < length; j++) {
                                JSONObject obj = jsonResult.getJSONObject(j);
                                GiftBag giftBag = new GiftBag();
                                giftBag.setId(obj.getLong("id"));
                                giftBag.setGiftName(obj.getString("giftname"));
                                giftBag.setGiftContent(obj.getString("giftcontent"));
                                giftBag.setTotalNumber(obj.getInt("totalnumber"));
                                giftBag.setLeftNumber(obj.getInt("leftnumber"));
                                giftBag.setExtstatus(obj.getInt("extstatus"));
                                giftBag.setIconUrl(obj.getString("img85x85"));
                                giftBag.setExistscardnumber(obj.getString("existscardnumber"));
                                if ("null".equals(giftBag.getExistscardnumber())) {
                                    giftBag.setExistscardnumber("");
                                }
                                list.add(giftBag);
                            }
                            response.setOk(true);
                            response.setGiftbagList(list);
                        }
                    }
                }
            } catch (Exception e) {

            }

        }
    }
}
